<!DOCTYPE html>
<html lang="en">
<title>权限中心-新增用户</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<link href="${baseUrl}css/plugins/awesome-bootstrap-checkbox/awesome-bootstrap-checkbox.css" rel="stylesheet">
<link href="${baseUrl}css/plugins/iCheck/custom.css" rel="stylesheet">
<style>
    .is-checkbox {
        min-height: 200px
    }

    .is-checkbox .checkbox {
        width: 100%
    }
</style>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5 class="pull-left">新增用户</h5>
                </div>
                <div class="ibox-content">
                    <form method="post" name="user_add_form" class="form-horizontal">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户姓名</label>
                            <div class="col-sm-10"><input type="text" id="userName" name="userName" class="form-control"></div>
                            <input type="hidden" name="menusId" id="menusId">
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">手机号码</label>
                            <div class="col-sm-10"><input type="text" id="mobile" name="mobile" class="form-control"></div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">登录密码</label>
                            <div class="col-sm-10"><input type="text" id="password" name="password" class="form-control"></div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">用户类型</label>
                            <div class="col-sm-10">
                                <select id="roleId" class="form-control m-b" name="account">
                                    <option value="">请选择</option>
                                    <#list role_list as item>
                                        <option value="${item.id}">${item.roleName}</option>
                                    </#list>
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                        <div class="form-group"><label class="col-sm-2 control-label">所属角色</label>
                            <div class="col-sm-10">
                                <div class="row">
                                    <#list menu_list as item>
                                        <#if item.parentNo == 0>
                                            <div class="col-sm-3 is-checkbox">
                                                <div class="checkbox checkbox-primary">
                                                    <input id="checkbox-${item.id}" value="${item.id}" type="checkbox" class="is-checkedAll">
                                                    <label for="checkbox-${item.id}">${item.menuName}</label>
                                                </div>
                                                <div style="margin-left: 20px">
                                                    <#list menu_list as item_detail>
                                                        <#if item.id == item_detail.parentNo>
                                                            <div class="checkbox checkbox-primary">
                                                                <input id="checkbox-${item_detail.id}" value="${item_detail.id}" name="checkbox" type="checkbox">
                                                                <label for="checkbox-${item_detail.id}">${item_detail.menuName}</label>
                                                            </div>
                                                        </#if>
                                                    </#list>
                                                </div>
                                            </div>
                                        </#if>
                                    </#list>
                                </div>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>
                    </form>
                </div>
                <div class="ibox-footer overflow">
                    <button type="button" onclick="javascript:window.location.href=history.go(-1)" class="btn btn-default pull-left">取消添加</button>
                    <button type="button" id="user_add_b" class="btn btn-primary pull-right">确认添加</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $(document).ready(function () {
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green',
            radioClass: 'iradio_square-green'
        });
        //全选
        $(".is-checkedAll").click(function () {
            var $subBox = $(this).parents(".is-checkbox").find("input[name='checkbox']");
            if (!this.checked) {
                for (var i = 0; i < $subBox.length; i++) {
                    $subBox[i].checked = false;
                }
            } else {
                for (var j = 0; j < $subBox.length; j++) {
                    $subBox[j].checked = true;
                }
            }
        });
        $("input[name='checkbox']").click(function () {
            var $checkAll = $(this).parents(".is-checkbox").find(".is-checkedAll");
            var $subBox = $(this).parents(".is-checkbox").find("input[name='checkbox']");
            if ($subBox.length == $(".is-checkbox input[name='checkbox']:checked").length) {
                $checkAll.attr("checked", true);
            } else {
                $checkAll.attr("checked", false);
            }
        });

        $("#user_add_b").click(function () {
            var userName = Trim($("#userName").val(), 'g');
            var mobile = Trim($("#mobile").val(), 'g');
            var password = Trim($("#password").val(), 'g');
            if (userName == '') {
                swal("用户姓名不能为空!",'','error');
                $("#userName").focus();
                return;
            }
            if (mobile == '') {
                swal("手机不能为空!",'','error');
                $("#mobile").focus();
                return;
            }
            if (password == '') {
                swal("密码不能为空!",'','error');
                $("#password").focus();
                return;
            }
            var roleId = $("#roleId option:selected").val();
            if (roleId == '') {
                swal("请选择用户类型!",'','error');
                $("#roleId").focus();
                return;
            }
            var menusId = "";
            $('input[type="checkbox"]:checked').each(function () {
                menusId += $(this).val() + ",";
            });
            $("#menusId").val(menusId);
            var url = 'user_add';
            var d = $("form").serialize();

            swal({
                title: '确认添加',
                text: "",
                type: 'question',
                showCancelButton: true,
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, 确认添加',
                cancelButtonText: 'No, 再次确认'
            }).then(function () {
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: d,
                    async: false,
                    success: function (returndata) {
                        if (returndata > 0) {
                            swal("添加成功!",'页面即将跳转,请稍后...','success');
                            window.setTimeout(function () {
                                window.history.go(-1);
                            }, 1000)
                        } else if (returndata == -1) {
                            swal("手机号码已被注册!",'','info');
                            $("#mobile").focus();
                        }
                    },
                    error: function (returndata) {
                        swal(returndata,'','error');
                    }
                });
            }, function () {
            });


        });
    });


    function Trim(str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g")
            result = result.replace(/\s/g, "");
        return result;
    }
</script>

</@body.page>

</html>